﻿using GBT.SystemBusinessCore;
using GBT.SystemEnties;
using SqlSugar;
using System;

namespace QueryDataLogic.Core
{
    public class TakeFoodRecordManage
    {
        BasicInfo bi = new BasicInfo();
        DbContext<TakeFoodRecord> dbContext = null;

        public object GetTakeFoodRecord(System_TakeFoodRecord takeFoodRecord, ref int totalNumber, ref int totalPage)
        {
            DateTime dt = DateTime.Now.Date;
            string dbName = bi.GetDBName(takeFoodRecord.DistrictID);
            dbContext = new DbContext<TakeFoodRecord>(dbName);
            var db = dbContext.Db;
            var takeFoodRecordList = db.Queryable<TakeFoodRecord, MealUser, DiningHall, MealTime>((tfr, mu, dh, mt) => new object[] {
                JoinType.Left, tfr.UserID == mu.UserID,
                JoinType.Left, tfr.HallID == dh.HallId,
                JoinType.Left, tfr.MealID == mt.MealId,
            })
            .Where((tfr, mu, dh, mt) => tfr.CreationDate >= dt)
            .WhereIF(takeFoodRecord.UserID != 0, (tfr, mu, dh, mt) => tfr.UserID == takeFoodRecord.UserID)
            .WhereIF(takeFoodRecord.HallID != 0, (tfr, mu, dh, mt) => tfr.HallID == takeFoodRecord.HallID)
            .WhereIF(takeFoodRecord.MealID != 0, (tfr, mu, dh, mt) => tfr.MealID == takeFoodRecord.MealID)
            .WhereIF(takeFoodRecord.Creator != 0, (tfr, mu, dh, mt) => tfr.Creator == takeFoodRecord.Creator)
            .OrderBy((tfr, mu, dh, mt) => tfr.CreationDate, OrderByType.Desc)
            .Select((tfr, mu, dh, mt) => new {
                tfr.ID,
                tfr.UserID,
                tfr.HallID,
                tfr.MealID,
                mu.Number,
                mu.Name,
                mu.nickName,
                dh.HallName,
                dh.HallNumber,
                mt.MealNumber,
                mt.MealName,
                tfr.Price,
                tfr.Qty,
                tfr.CreationDate,
            })
            .ToPageList(takeFoodRecord.PageIndex, takeFoodRecord.PageSize, ref totalNumber, ref totalPage);
            return takeFoodRecordList;
        }
    }
}
